package org.jf.tool.fx.jftoolfx.tools.cron.controller;

import com.alibaba.fastjson2.util.DateUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import org.jf.tool.fx.jftoolfx.common.BaseController;
import org.jf.tool.fx.jftoolfx.util.CronUtil;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * cron 工具
 *
 * @author jifeng_zhang
 * @date 2025/10/9 上午11:41
 */
public class CronController extends BaseController {

    @FXML
    public VBox main;
    @FXML
    public TabPane tabPane;
    @FXML
    public VBox resultVBox;
    @FXML
    protected TextField cronSecondText;
    @FXML
    protected TextField cronMinuteText;
    @FXML
    protected TextField cronHourText;
    @FXML
    protected TextField cronDayText;
    @FXML
    protected TextField cronMonthText;
    @FXML
    protected TextField cronWeekText;
    @FXML
    protected TextField cronYearText;
    @FXML
    protected TextField cronText;
    @FXML
    protected TextField runCountText;
    @FXML
    protected TextArea resultText;
    @FXML
    protected SecondController secondController;
    @FXML
    protected MinuteController minuteController;
    @FXML
    protected HourController hourController;
    @FXML
    protected DayController dayController;
    @FXML
    protected MonthController monthController;
    @FXML
    protected WeekController weekController;
    @FXML
    protected YearController yearController;
    @FXML
    protected ValidateController validateController;

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
        // 使用 ChangeListener
        tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
            @Override
            public void changed(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {
                if (newValue != null) {
                    String text = newValue.getText();
                    if ("校验".equalsIgnoreCase(text) || "说明".equalsIgnoreCase(text)) {
                        resultVBox.setVisible(false);
                        resultVBox.setManaged(false);
                        resultText.setVisible(false);
                        resultText.setManaged(false);
                        // 设置 VBox 的 grow 属性为 ALWAYS，使其填充剩余空间
                        VBox.setVgrow(tabPane, Priority.ALWAYS);
                    } else {
                        resultVBox.setVisible(true);
                        resultVBox.setManaged(true);
                        resultText.setVisible(true);
                        resultText.setManaged(true);
                    }
                }
            }
        });
        secondController.setCronController(this);
        minuteController.setCronController(this);
        hourController.setCronController(this);
        dayController.setCronController(this);
        monthController.setCronController(this);
        weekController.setCronController(this);
        yearController.setCronController(this);
        validateController.setCronController(this);
    }

    public void run(ActionEvent actionEvent) {
        String cron = cronText.getText();
        // cron 校验
        if (cron == null || cron.isEmpty()) {
            resultText.setText("请输入cron表达式");
        }
        int count = 5;
        String runCount = runCountText.getText();
        try {
            count = Integer.parseInt(runCount);
            if (count < 1 || count > 100) {
                count = 5;
            }
        } catch (NumberFormatException e) {
            // ignore
        }
        runCountText.setText(String.valueOf(count));
        try {
            List<Date> nextExecutionTimes = CronUtil.getNextExecutionTimes(cron, count);
            resultText.setText("");
            for (Date nextExecutionTime : nextExecutionTimes) {
                resultText.appendText(DateUtils.formatYMDHMS19(nextExecutionTime) + "        ");
            }
        } catch (Exception e) {
            resultText.setText(e.getMessage());
        }
    }

    public void copy(ActionEvent actionEvent) {
        copy(cronText.getText());
    }

}
